import { getCookie } from "@/utils/cookie.js";
const Base64 = require('js-base64').Base64
const initState = () => {
    return {
        signData: {
            isShow: false, //是否显示弹窗
            cardTransition: "",
            title: "Sign-In", //弹窗标题
            btn: "next", //按钮文字
            remember: false, //是否记住
            agree: false, //是否同意协议
            email: {
                //邮箱
                title: "Email address: ", //输入框标题
                placeholder: "Enter email", //输入框默认提示
                tip: "Please enter an valid email address.Email address not yet registered.", //错误提示
                value: "", //输入框的值
                name: "email", //正则名称
            },
            password: {
                //密码
                title: "Password: ",
                placeholder: "Password",
                tip: "Please enter your password.",
                asyncTip:
                    "The email address and password don’t match, please try again.", //动态校验提示
                value: "",
                name: "password",
                type: "password",
            },
        },
    };
};

const state = initState();

const mutations = {
    //打开登录弹窗
    openSignPop(state, type) {
        state.signData.isShow = true;
        state.signData.cardTransition = type ? type : 'contentEnterBottom';
    },
    //关闭登录弹窗
    closeSignPop(state, type) {
        state.signData.cardTransition = type ? type : 'contentLeaveBottom';
        state.signData.isShow = false;
        setTimeout(() => {
            this.commit('sign/resetState');
        });
    },
    //获取记住密码和账号
    getRemember(state) {
        // 在页面加载时从cookie获取登录信息
        let email = getCookie("email");
        let password = getCookie("password");
        // 如果存在赋值给表单，并且将记住密码勾选
        if (email && password) {
            state.signData.email.value = email;
            state.signData.password.value = Base64.decode(password);
            state.signData.remember = true;
        }
    },
    resetState(state) {
        Object.assign(state, initState());
    },
};

const actions = {
};

const getters = {};

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
};